﻿using System;
using System.Collections.Generic;
using UnityEngine;

public static class PrimitiveMeshes
{
    private static Dictionary<PrimitiveType, Mesh> primitiveMeshes = new Dictionary<PrimitiveType, Mesh>();

    public static GameObject CreatePrimitive(PrimitiveType type, bool withCollider)
    {
        if (withCollider)
        {
            return GameObject.CreatePrimitive(type);
        }
        GameObject obj2 = new GameObject(type.ToString());
        obj2.AddComponent<MeshFilter>().sharedMesh = GetPrimitiveMesh(type);
        obj2.AddComponent<MeshRenderer>();
        return obj2;
    }

    private static Mesh CreatePrimitiveMesh(PrimitiveType type)
    {
        GameObject obj2 = GameObject.CreatePrimitive(type);
        Mesh sharedMesh = obj2.GetComponent<MeshFilter>().sharedMesh;
        UnityEngine.Object.DestroyImmediate(obj2);
        primitiveMeshes[type] = sharedMesh;
        return sharedMesh;
    }

    public static Mesh GetPrimitiveMesh(PrimitiveType type)
    {
        if (!primitiveMeshes.ContainsKey(type))
        {
            CreatePrimitiveMesh(type);
        }
        return primitiveMeshes[type];
    }
}

